﻿@using Masa.Blazor.Presets
@using WPFBlazorChat.Services
@using WPFBlazorChat.Messages

<MApp>

    <!--新增的Masa.Blazor Tab案例代码开始-->
    <MCard>
        <MToolbar Color="cyan" Dark Flat @ondblclick="WindowService.Maximize" @onmouseup="WindowService.StopMove" @onmousedown="WindowService.StartMove">
            <MTabs @bind-Value="tab"
                   AlignWithTitle
                   SliderColor="yellow">
                @foreach (var item in items)
                {
                    <MTab Value="item">
                        @item
                    </MTab>
                }
            </MTabs>

            <div style="flex-grow: 1"></div>
            <button class="titlebar-btn" onclick="alert('js alert: settings pressed');">
                <img src="svg/settings.svg"/>
            </button>
            <button class="titlebar-btn" @onclick="WindowService.Minimize">
                <img src="svg/minimize.svg"/>
            </button>
            <button class="titlebar-btn" @onclick="WindowService.Maximize">
                @if (WindowService.IsMaximized())
                {
                    <img src="svg/restore.svg"/>
                }
                else
                {
                    <img src="svg/maximize.svg"/>
                }
            </button>
            <button class="titlebar-cbtn" @onclick="() => WindowService.Close(false)">
                <img src="svg/dismiss.svg"/>
            </button>
        </MToolbar>

        <MTabsItems @bind-Value="tab">
            @foreach (var item in items)
            {
                <MTabItem Value="item">
                    <MCard Flat>
                        <MCardText>@text</MCardText>
                    </MCard>
                </MTabItem>
            }
        </MTabsItems>
    </MCard>
    <!--新增的Masa.Blazor Tab案例代码结束-->

    <MCard>
        <MRow>
            <MCol>
                <MButton class="mx-2" Fab Dark Color="indigo" OnClick="OpenNewSecondView">
                    <MIcon>mdi-plus</MIcon>
                </MButton>
            </MCol>
            <MCol>
                <MButton class="mx-2" Fab Small Dark Color="pink" OnClick="SendNumber">
                    <MIcon>mdi-heart</MIcon>
                </MButton>
            </MCol>
        </MRow>
    </MCard>

    <!--确认对话框开始-->
    <PConfirm Visible="_showComfirmDialog"
              Title="子窗体来回应了"
              Type="AlertTypes.Warning"
              OnCancel="() => _showComfirmDialog = false"
              OnOk="() => _showComfirmDialog = false">
        说你别没事一直发，它们烦！
    </PConfirm>
    <!--确认对话框结束-->
</MApp>

@code {

    #region Masa.Blazor Tab案例C#代码

    StringNumber tab;

    List<string> items = new()
    {
        "web", "shopping", "videos", "images", "news",
    };

    string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.";

    #endregion

    // 是否显示确认对话框
    bool _showComfirmDialog;

    protected override void OnInitialized()
    {
        WindowService.Init();

    // 订阅子窗体响应的消息，它已经收到消息了，我可以休息下再发
        Messenger.Default.Subscribe<ReceivedResponseMessage>(this, msg =>
        {
            this.InvokeAsync(() => { _showComfirmDialog = true; });
            this.StateHasChanged();
        }, ThreadOption.UiThread);
        base.OnInitialized();
    }

    void OpenNewSecondView()
    {
        Messenger.Default.Publish(this, new OpenSecondViewMessage(this));
    }

    void SendNumber()
    {
        Messenger.Default.Publish(this, new SendRandomDataMessage(this, DateTime.Now.Millisecond));
    }

}